// 判断是否是空字符串
let isEmptyString = (str) => {
  return str === '' || str.trim() === ''
}

// 获得args的数据类型
let getTypeOfParam = (args) => {
  // [object String]
  return Object.prototype.toString.call(args).slice(8, -1)
}

// 判断args是否是字符串
let isString = (args) => {
  return Object.prototype.toString.call(args).slice(8, -1) === 'String'
}

// 字符串反转
let reverseString = (str) => {
  if (!isString(str)) {
    return console.log('请传递字符串参数')
  }
  if (isEmptyString(str)) {
    return ''
  }
  return str.split('').reverse().join('')
}

// 获得url中的查询参数
let getQuery = (url) => {
  // 'http://146.56.230.35:3007/users/login?username=admin&mobile=13988881111&password=123456&id=1&age=20&gender=女'
  let query = {}
  if (isEmptyString(url)) {
    console.log('url不能是空字符串')
    return query
  }
  let index = url.indexOf('?')
  if (index === -1) {
    console.log('当前url中没有查询参数')
    return query
  }
  let queryString = url.substring(index + 1)
  if (isEmptyString(queryString)) {
    console.log('当前url中没有查询参数')
    return query
  }
  // ['username=admin','mobile=13988881111','password=123456','id=1','age=20','gender=女']
  let arr = queryString.split('&')
  arr.forEach((item) => {
    let list = item.split('=')
    console.log(list)
    // 属性名称
    let property = list[0]
    // 属性值
    let value = list[1]
    // 把字符串转换为数字，如果转换不成功说明value是字符串，value不是数字字符串
    let rt = Number(value)
    // 把数字字符串转换为数字
    if (!Number.isNaN(rt) && property !== 'password' && property !== 'mobile') {
      value = rt
    }
    // 把属性和属性对应的值赋值给对象query
    query[property] = value
  })
  return query
}

// 日期格式化
const formatTime = (date) => {
  const year = date.getFullYear()
  const month = date.getMonth() + 1
  const day = date.getDate()
  const hour = date.getHours()
  const minute = date.getMinutes()
  const second = date.getSeconds()
  return `${[year, month, day].map(formatNumber).join('-')} ${[hour, minute, second].map(formatNumber).join(':')}`
}

// 不够2位加0
const formatNumber = (n) => {
  n = n.toString()
  return n[1] ? n : `0${n}`
}

// 到指定日期date还有多长时间
function getTime(date) {
  let dateNowTime = new Date().getTime()
  let date51Time = new Date(date).getTime()
  // let date51Time = new Date('2023-05-01 08:00:00').getTime()
  let times = date51Time - dateNowTime // 现在到5.1的毫秒数
  // 一天的毫秒数
  let dayTime = 86400 * 1000
  let days = Math.floor(times / dayTime)
  // 一个小时的毫秒数
  let hoursTime = 3600 * 1000
  let hours = Math.floor((times % dayTime) / hoursTime)
  // 一分钟的毫秒数
  let minTimes = 60 * 1000
  let mins = Math.floor((times % hoursTime) / minTimes)
  let secs = Math.floor((times % minTimes) / 1000)
  return days + '天 ' + hours + ':' + mins + ':' + secs
}

// 2022年10月14日 02:52:55 PM 星期五
function formatTime_1() {
  let date = new Date()
  let y = date.getFullYear()
  let m = date.getMonth() + 1
  let d = date.getDate()
  let h = date.getHours()
  let minu = date.getMinutes()
  let s = date.getSeconds()
  let flag = 'AM'
  if (h > 12) {
    h -= 12
    flag = 'PM'
  }
  let day = date.getDay()
  let arr = [y, m, d, h, minu, s, day].map(formatNumber)
  let str = `${arr[0]}年${arr[1]}月${arr[2]}日 ${arr[3]}:${arr[4]}:${arr[5]} ${flag} ${getDayOfWeek(day)}`
  return str
}

// 传递参数获得周中的天
function getDayOfWeek(n) {
  let week = ['星期一', '星期二', '星期三', '星期四', '星期五', '星期六', '星期日']
  return week[n - 1]
}
